<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/08/24
 * Time: 22:13
 * @link http://www.lmterp.cn
 */

namespace app\common\model;

/**
 * 匹配规则组
 * Class ChannelRulesGroup
 * @package app\common\model
 */
class OrdersRulesGroup extends BaseModel
{
    protected $pk = 'group_id';

    protected $autoWriteTimestamp = 'datetime';

    protected $insert = ['create_by', 'update_by'];

    protected $update = ['update_by'];

    /**
     * 物流规则
     * @var int
     */
    const REF_TYPE_LGS = 0;

    /**
     * 分仓规则
     * @var int
     */
    const REF_TYPE_WAREHOUSE = 10;

    public static $REF_TYPE = [
        self::REF_TYPE_LGS          => '物流规则',
        self::REF_TYPE_WAREHOUSE    => '分仓规则',
    ];

    protected function getRefTypeAttr($value)
    {
        return isset(self::$REF_TYPE[$value]) ? self::$REF_TYPE[$value] : $value;
    }


    public function scopeRefType($query, $type)
    {
        $query->where('ref_type', $type)->field(['group_id', 'group_name'])->order("sort desc");
    }

    /**
     * 关联规则详情
     * @return \think\model\relation\HasMany
     * @date 2021/08/24
     * @author longli
     */
    public function rules()
    {
        return $this->hasMany(OrdersRules::class, 'group_id', 'group_id');
    }
}